<?php

/**
 * This is the model class for table "{{like}}".
 *
 * The followings are the available columns in table '{{like}}':
 * @property integer $id
 * @property string $module
 * @property integer $module_id
 * @property integer $status_code
 * @property string $create_timezone
 * @property string $create_timestamp
 * @property string $create_datetime
 * @property string $create_date
 * @property string $create_time
 * @property integer $create_by
 * @property string $update_timezone
 * @property string $update_timestamp
 * @property string $update_datetime
 * @property string $update_date
 * @property string $update_time
 * @property integer $update_by
 */
class Like extends ActiveRecord {
    
    public $module_;
    public $moduleId_;

    /**
     * Returns the static model of the specified AR class.
     * @param string $className active record class name.
     * @return Like the static model class
     */
    public static function model($className = __CLASS__) {
        return parent::model($className);
    }

    /**
     * @return string the associated database table name
     */
    public function tableName() {
        return '{{like}}';
    }

    /**
     * @return array validation rules for model attributes.
     */
    public function rules() {
        // NOTE: you should only define rules for those attributes that
        // will receive user inputs.
        return array(
            array('module_, moduleId_', 'filter', 'filter'=>'trim'),
            array('module_', 'required', 'on'=>'create'),
            array('module_', 'length', 'max'=>45),
            array('moduleId_', 'numerical', 'allowEmpty'=>false, 'integerOnly'=>true, 'on'=>'create'),
        );
    }

    /**
     * @return array relational rules.
     */
    public function relations() {
        // NOTE: you may need to adjust the relation name and the related
        // class name for the relations automatically generated below.
        return array(
            // must included
            'creator' => array(self::BELONGS_TO, 'User', 'create_by'),
            'editor' => array(self::BELONGS_TO, 'User', 'update_by'),
            
        );
    }

    /**
     * @return array customized attribute labels (name=>label)
     */
    public function attributeLabels() {
        return array(

        );
    }

    protected function beforeSave(){
        if(parent::beforeSave()){
            if($this->isNewRecord){
                $this->module=strtolower($this->module_);
                $this->module_id=$this->moduleId_;
                $this->status_code = Option::STATUS_ACTIVE;
                $this->setCreateDateTime();
            }else{
                $this->setUpdateDateTime();
            }
            return true;
        }else
            return false;
    }
    
    protected function afterSave(){
        parent::afterSave();
        
        $this->saveBrowserLog();
    }
    
    public function addRecord(){
        return $this->save(true);
    }
    
    public function deleteRecord(){
        $this->status_code=Option::STATUS_DELETED;
        return $this->save(true);
    }
    
    
    /**
     * scopes
     */   
    public function scopes() {
        $t = $this->getTableAlias(false);
        return array(
            'published'=>array(
                'condition' => $t.'.status_code = :activeStatus'.$t,
                'params' => array(
                    ':activeStatus'.$t => Option::STATUS_ACTIVE,
                ),
            ),
            'ordered'=>array(
                'order'=>implode(',',array(
                    $t.'.create_time DESC',
                )),
            ),
            'accessible'=>array(
                'condition' => $t.'.status_code <> :deletedStatus'.$t,
                'params' => array(
                    ':deletedStatus'.$t => Option::STATUS_DELETED,
                ),
            ),
        );
    }

    public function module($module = null) {
        $t = $this->getTableAlias(false);
        $this->getDbCriteria()->mergeWith(array(
            'condition' => $t.'.module = :module'.$t,
            'params' => array(
                ':module'.$t => strtolower($module),
            ),
        ));
        return $this;
    }

    public function moduleId($id = null) {
        $t = $this->getTableAlias(false);
        $this->getDbCriteria()->mergeWith(array(
            'condition' => $t . '.module_id = :moduleId'.$t,
            'params' => array(
                ':moduleId'.$t => (int)$id,
            ),
        ));
        return $this;
    }
    
    public function userId($userId = null) {
        $t = $this->getTableAlias(false);
        $this->getDbCriteria()->mergeWith(array(
            'condition' => $t . '.create_by = :userId'.$t,
            'params' => array(
                ':userId'.$t => (int)$userId,
            ),
        ));
        return $this;
    }
    
    /**
     * Retrieves a list of models based on the current search/filter conditions.
     * @return CActiveDataProvider the data provider that can return the models based on the search/filter conditions.
     */
    public function search() {
        // Warning: Please modify the following code to remove attributes that
        // should not be searched.
        $t=$this->getTableAlias(false);
        $criteria = new CDbCriteria;

        $criteria->compare($t.'.module', strtolower($this->module_));
        $criteria->compare($t.'.create_by', $this->userId_);
        
        return new ActiveDataProvider($this->accessible(), array(
            'criteria' => $criteria,
        ));
    }
    
    public function getModuleName(){
        return $this->module;
    }
    
    public function getModuleId(){
        return $this->module_id;
    }

}